package web.req;

import common.consts.Actions;
import common.consts.Consts;
import common.validation.ManualValidator;
import common.validation.ValidationResult;
import web.Request;

/**
 * Created by aceyin on 15/6/23.
 */
public class PasswordRequest extends Request {
    private long id;
    private String action;
    private String mobile;
    private String token;
    private String captcha;
    private String opassword;
    private String password;
    private String password2;

    @Override
    public ValidationResult validate() {
        ValidationResult result = ManualValidator.create(this)
                .notBlank("action")
                .validate();
        if (result.isFailed()) {
            return result;
        }

        if (Actions.Password.RESET.name().equalsIgnoreCase(action)) {
            return validate4ResetPassword();
        }
        return validate4ChangePassword();
    }

    private ValidationResult validate4ChangePassword() {
        ValidationResult result = ManualValidator.create(this)
                .notBlank("opassword")
                .notBlank("password")
                .notBlank("password2")
                .validate();

        if (result.isFailed()) {
            return result;
        }

        boolean pwdEquals = password.equals(password2);
        if (!pwdEquals) {
            result.setSuccess(false);
            result.setMessage(Consts.i18nKey.TOW_PASSWORD_NOT_EQUALS);
        }
        return result;
    }

    private ValidationResult validate4ResetPassword() {
        ValidationResult result = ManualValidator.create(this)
                .notBlank("mobile")
                .notBlank("token")
                .notBlank("captcha")
                .notBlank("password")
                .validate();

        return result;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public String getOpassword() {
        return opassword;
    }

    public void setOpassword(String opassword) {
        this.opassword = opassword;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public String getCaptcha() {
        return captcha;
    }

    public void setCaptcha(String captcha) {
        this.captcha = captcha;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPassword2() {
        return password2;
    }

    public void setPassword2(String password2) {
        this.password2 = password2;
    }
}
